﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kinect.Toolbox;
using Kinect.Toolbox.Analyzers;
using Kinect.Toolbox.Collectors;
using Microsoft.Kinect;
using Microsoft.Practices.Unity;
using Model;

namespace Gui.ViewModels
{
    public class ControlPanelViewModel : INotifyPropertyChanged
    {
        private readonly PostureAnalyzer _algorithmicPostureDetector;

        private readonly IViewConfiguration _viewConfiguration;

        public bool IsHandNavigationMode
        {
            get { return _viewConfiguration.IsHandNavigationMode; }
            private set
            {
                _viewConfiguration.IsHandNavigationMode = value;
                OnPropertyChanged("IsHandNavigationMode");
            }
        }

        public bool IsHeadNavigationMode
        {
            get { return _viewConfiguration.IsHeadNavigationMode; }
            private set
            {
                _viewConfiguration.IsHeadNavigationMode = value;
                OnPropertyChanged("IsHeadNavigationMode");
            }
        }

        public ControlPanelViewModel()
        {
            _viewConfiguration = DependencyFactory.Container.Resolve<IViewConfiguration>();
            var kinectSensor = DependencyFactory.Container.Resolve<KinectSensor>();

            var framesCollector = new FramesCollector(kinectSensor);
            _algorithmicPostureDetector = new PostureAnalyzer(framesCollector);
            _algorithmicPostureDetector.PostureDetected += AlgorithmicPostureDetectorOnPostureDetected;
        }

        private void AlgorithmicPostureDetectorOnPostureDetected(object sender, PostureEventArgs args)
        {
            switch (args.Posture)
            {
                case PosturesEnum.LeftHandOverHead:
                    {
                        IsHandNavigationMode = !IsHandNavigationMode;
                    }
                    break;
                case PosturesEnum.LeftHello:
                    {
                        IsHeadNavigationMode = !IsHeadNavigationMode;
                    }
                    break;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
